package com.sl.au.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.ChanceTotalView;
import com.sl.au.entity.EXDEcustomerViewTotal;
import com.sl.au.entity.SaleArea;
import com.sl.au.entity.User;
import com.sl.au.service.ChanceTotalViewService;
import com.sl.au.service.OptionService;
import com.sl.au.service.SaleAreaService;

@RestController
@RequestMapping(value = "/ui/chanceTotal")
public class ChanceTotalController extends BaseController {
	
	@Autowired
	OptionService optionService;
	
	@Autowired
	SaleAreaService saleAreaService;
	
	@Autowired
	ChanceTotalViewService chanceTotalViewService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("chanceTotal/ChanceTotalIndex");
		//
		System.out.println(request.getParameter("deptName"));
		String[] areaNames = null;
		String[] superiors = null;
		if(StringUtils.hasText(request.getParameter("areaName"))){
			areaNames = request.getParameter("areaName").toString().split(",");//片区
		}
		if(StringUtils.hasText(request.getParameter("deptName"))){
			superiors = request.getParameter("deptName").toString().split(",");//部门
		}
		List<SaleArea> orderAreas = saleAreaService.getRepository().getAllSaleArea();
		List<SaleArea> SaleOrderAreas = new ArrayList<SaleArea>();//销售
		List<SaleArea> CompleteOrderAreas = new ArrayList<SaleArea>();//成套
		List<SaleArea> ForeignOrderAreas = new ArrayList<SaleArea>();//外贸
		List<SaleArea> shOrderAreas = new ArrayList<SaleArea>();//售后服务处
		for(SaleArea orderArea : orderAreas){
			orderArea.setIsSelected(false);
			if(orderArea.getSuperior().equals("销售处")){
				if(superiors!=null&&superiors.length>0){
					if(areaNames!=null&&areaNames.length>0){
						for(int i=0;i<areaNames.length;i++){
							if(areaNames[i].equals(orderArea.getSuperiorArea()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<superiors.length;i++){
							if(superiors[i].equals(orderArea.getSuperior()))
								orderArea.setIsSelected(true);
						}
					}
				}
				if(orderArea.getAreaName().equals("销售处-分管领导")){
				}else{
					if(orderArea.getAreaName().equals("科长")){
					}else{
						SaleOrderAreas.add(orderArea);
					}
				}
			}
			if(orderArea.getSuperior().equals("成套处")){
				if(superiors!=null&&superiors.length>0){
					if(areaNames!=null&&areaNames.length>0){
						for(int i=0;i<areaNames.length;i++){
							if(areaNames[i].equals(orderArea.getSuperiorArea()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<superiors.length;i++){
							if(superiors[i].equals(orderArea.getSuperior()))
								orderArea.setIsSelected(true);
						}
					}
				}
				if(orderArea.getAreaName().equals("其他")){
				}else{
					if(orderArea.getAreaName().equals("成套处分管领导")){
					}else{
						CompleteOrderAreas.add(orderArea);
					}
				}
			}
			if(orderArea.getSuperior().equals("外贸处")){
				if(superiors!=null&&superiors.length>0){
					if(areaNames!=null&&areaNames.length>0){
						for(int i=0;i<areaNames.length;i++){
							if(areaNames[i].equals(orderArea.getSuperiorArea()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<superiors.length;i++){
							if(superiors[i].equals(orderArea.getSuperior()))
								orderArea.setIsSelected(true);
						}
					}
				}
				if(orderArea.getAreaName().equals("外贸处-分管领导")){
				}else{
					ForeignOrderAreas.add(orderArea);
				}
			}
			
			if(orderArea.getSuperior().equals("售后服务处")){
				if(superiors!=null&&superiors.length>0){
					if(areaNames!=null&&areaNames.length>0){
						for(int i=0;i<areaNames.length;i++){
							if(areaNames[i].equals(orderArea.getSuperiorArea()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<superiors.length;i++){
							if(superiors[i].equals(orderArea.getSuperior()))
								orderArea.setIsSelected(true);
						}
					}
				}
				if(orderArea.getAreaName().equals("售后服务处-分管领导")){
				}else{
					shOrderAreas.add(orderArea);
				}
			}
		}
		modelAndView.addObject("SaleOrderAreas", SaleOrderAreas);
		modelAndView.addObject("CompleteOrderAreas", CompleteOrderAreas);
		modelAndView.addObject("ForeignOrderAreas", ForeignOrderAreas);
		modelAndView.addObject("shOrderAreas", shOrderAreas);
		
		modelAndView.addObject("industryInfos", optionService.getRepository().findByGroup("行业"));//行业
		//modelAndView.addObject("industryInfo1", request.getParameter("industryName")!=null ? request.getParameter("industryName") : "");//行业
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String s = df.format(new Date());
		Calendar a=Calendar.getInstance(); 
		int aa = a.get(Calendar.YEAR);
		modelAndView.addObject("startDate", request.getParameter("beginDate")!= null ? request.getParameter("beginDate") : a.get(Calendar.YEAR)+"-01-01");//开始时间
		modelAndView.addObject("endDate", request.getParameter("endDate")!=null ? request.getParameter("endDate") : df.format(new Date()));//结束时间
		//modelAndView.addObject("nameDate", request.getParameter("dateTimeType")!=null?request.getParameter("dateTimeType"):"");//时间类型
		
		//modelAndView.addObject("isSuccess", request.getParameter("isSuccess") != null ? request.getParameter("isSuccess") : "" );//部门柱状图是否成功
		if(request.getParameter("isSuccess") != null){
			modelAndView.addObject("isSuccess", request.getParameter("isSuccess").toString().equals("所有") ? "" : request.getParameter("isSuccess") );//状态分析
		}else{
			modelAndView.addObject("isSuccess", "" );//状态分析	
		}
		modelAndView.addObject("chanceCount", request.getParameter("chanceCount") != null ? request.getParameter("chanceCount") : "" );//信用评分
		return modelAndView;
	}
	
	//获取主界面列表中的数据
		@RequestMapping(value = "getChanceTotalPage", method = RequestMethod.GET)
		public Map<String, Object> getChanceTotalPage(HttpServletRequest request) {
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> qMap = new HashMap<String, Object>();
			Map<String, Object> condition = new HashMap<String, Object>();
			Map<String, Object>numMap=new HashMap<String, Object>();
			int start = Integer.parseInt(request.getParameter("start"));
			int length = Integer.parseInt(request.getParameter("length"));
			Account a = this.getMyAccount();
			String dataFilter = "";
			User user = a.getUser();
			this.getQueryParams(qMap,numMap);
			condition.put("qMap", qMap);
			condition.put("numMap", numMap);
			
			String userId = a.getUser().getId();
			String Superior = a.getUser().getSaleArea().getSuperior();//权限部门
			String areaName = a.getUser().getSaleArea().getAreaName();
			if (userId == null) {
				userId = "";
			}
			if (Superior == null) {
				Superior = "";
			}
			if (areaName == null) {
				areaName = "";
			}
			/*if (dataFilter == null) {
				dataFilter = "";
			}*/
			condition.put("userId", userId);
			condition.put("superior", Superior);
			condition.put("areaName", areaName);
			condition.put("dataFilter", dataFilter);
			List<ChanceTotalView> contracts = chanceTotalViewService.getChanceTotal(start, length, condition, user, "createTime", "desc");
			List<ChanceTotalView> contractsum = chanceTotalViewService.getChanceTotal(0, Integer.MAX_VALUE, condition, user, "createTime","desc");
			long count = contractsum.size();
			map.put("data", contracts);
			map.put("draw", request.getParameter("draw"));
			map.put("recordsFiltered", count);
			map.put("recordsTotal", count);
			return map;
		}
		
		/**
		 * hql
		 * @param qMap
		 * @param numMap
		 */
			private void getQueryParams(Map<String, Object> qMap,Map<String, Object>numMap){
				String names[]={"sSuperiorArea","business","chancePriority"};//部门-片区，行业，重要程度
				String dates[] = {"createTime"};
				for(String name:names){
					System.out.println(request.getParameter(name));
					String aa =  request.getParameter(name);
					if(StringUtils.hasText(request.getParameter(name))){
						String[] a = request.getParameter(name).toString().split(",");
						String b = "";
						for(int i=0;i<a.length;i++){
							b += "'" + a[i] + "',";
						}
						b = b.substring(0, b.length()-1);
						numMap.put(name, b);
					}
					
				}
				//String ab = request.getParameter("creditLevel").toString();
				//System.out.println(ab+" ");
				if(StringUtils.hasText(request.getParameter("orderAmount"))){//预计订货量
					numMap.put("orderAmount", request.getParameter("orderAmount").toString());
				}
				if(StringUtils.hasText(request.getParameter("name"))){//机会名称
					System.out.println(request.getParameter("name"));
					numMap.put("name", request.getParameter("name").toString());
				}
				if(StringUtils.hasText(request.getParameter("isSucceed"))){//执行状态
					System.out.println(request.getParameter("isSucceed"));
					numMap.put("isSucceed", request.getParameter("isSucceed").toString());
				}
				for(String date:dates){
					if(null!=this.getDateQueryItem(date)){
						qMap.put(date,this.getDateQueryItem(date));
					}
				}
				
			}
	
}
